#include "parafluidsimulation.h"
#include "parafluidsimulationsettingswidget.h"
#include "parafluidvector.h"

using namespace ParaFluid;

SimulationSettingsWidget::~SimulationSettingsWidget()
{
	/* does nothing */
}

SimulationSettingsWidget::SimulationSettingsWidget(QWidget * parent)
	: QWidget(parent)
{
	connect(&(Simulation::instance()), SIGNAL(reseted()), this, SLOT(reset()));
	
	setupUi(this);
	
	spinBoxBuckets->setValue(Simulation::defaultBuckets());
	spinBoxAtoms->setValue(Simulation::defaultAtoms());
	doubleSpinBoxMass->setValue(Simulation::defaultSubstance().mass);
	doubleSpinBoxPressureFactor->setValue(Simulation::defaultSubstance().pressureFactor);
	doubleSpinBoxRestDensity->setValue(Simulation::defaultSubstance().restDensity);
	doubleSpinBoxViscosityFactor->setValue(Simulation::defaultSubstance().viscosityFactor);
	doubleSpinBoxTimeStep->setValue(Simulation::instance().sph().timeStep());
	doubleSpinBoxSmoothingDistance->setValue(Simulation::instance().sph().smoothingDistance());
	doubleSpinBoxGravityX->setValue(Simulation::instance().sph().gravity().x);
	doubleSpinBoxGravityY->setValue(Simulation::instance().sph().gravity().y);
	doubleSpinBoxGravityZ->setValue(Simulation::instance().sph().gravity().z);
	
	connect(doubleSpinBoxTimeStep, SIGNAL(valueChanged(double)), this, SLOT(setTimeStep()));
	connect(doubleSpinBoxSmoothingDistance, SIGNAL(valueChanged(double)), this, SLOT(setSmoothingDistance()));
	connect(doubleSpinBoxGravityX, SIGNAL(valueChanged(double)), this, SLOT(setGravity()));
	connect(doubleSpinBoxGravityY, SIGNAL(valueChanged(double)), this, SLOT(setGravity()));
	connect(doubleSpinBoxGravityZ, SIGNAL(valueChanged(double)), this, SLOT(setGravity()));
	
	reset();
}

int SimulationSettingsWidget::buckets() const
{
	return spinBoxBuckets->value();
}

int SimulationSettingsWidget::atoms() const
{
	return spinBoxAtoms->value();
}

Substance SimulationSettingsWidget::substance() const
{
	return Substance(
		doubleSpinBoxMass->value(),
		Simulation::defaultSubstance().color,
		doubleSpinBoxPressureFactor->value(),
		doubleSpinBoxRestDensity->value(),
		doubleSpinBoxViscosityFactor->value()
	);
}

void SimulationSettingsWidget::reset()
{
	setTimeStep();
	setSmoothingDistance();
	setGravity();
}

void SimulationSettingsWidget::setTimeStep()
{
	Simulation::instance().sph().setTimeStep(doubleSpinBoxTimeStep->value());
}

void SimulationSettingsWidget::setSmoothingDistance()
{
	Simulation::instance().sph().setSmoothingDistance(doubleSpinBoxSmoothingDistance->value());
}

void SimulationSettingsWidget::setGravity()
{
	Simulation::instance().sph().setGravity(
		Vector(
			doubleSpinBoxGravityX->value(),
			doubleSpinBoxGravityY->value(),
			doubleSpinBoxGravityZ->value()
		)
	);
}
